import { ref, computed, useId } from "vue";
import { defineStore } from "pinia";

export const useCounterStore = defineStore(
  "counter",
  () => {
    const workPackId = ref(61695);
    const sign = ref(
      "gJ5cLQ5%2BnJzcHgcun4WbPSOq9c8zmGqlKNoa5woK%2B87E%2FM%2BJhsg6hVyO6nV9jCNVgt9ssb87kCVCTYYIJIEy5U1wc%2Bo2VlR9R7l0kzflAWSmlTSsF%2FAlsrfXgmOB64sZJAHtOYmaKJ%2FALvD1tbBumqEcb6OW0tJhMskeAxM8IOI%3D"
    );
    const createTime = ref();
    const lastSubmitTime = ref();
    const lastChangeKeyNum = ref();
    const idsList = ref([]);
    const changeKeyList = ref([]);
    const changeKeyIdList = ref([]);
    const noOperationNum = ref(0);
    const dataList = ref();
    //目录树
    const menuTreeList = ref();
    //
    const moveTreeList = ref();
    const lineColor = ref("#ff0000");
    const color = ref();
    const level = ref();
    const cropExpandSize = ref(0);
    const lineWidth = ref(1);
    const lineStyle = ref("solid");
    const checkList = ref([]);
    const key = ref("");
    const lastNodeValue = ref("");
    const value = ref("");
    const list = ref();
    const processTypeName = ref("");
    const workUserName = ref("");
    const taskName = ref("");
    const receiveTime = ref("");
    const workUserDomain = ref("");
    const rollbackKeys = ref();

    const images = computed(() => {
      // 检查哪些条件被设置
      const conditions = {
        color: !!color.value,
        level: !!level.value,
        checkList: checkList.value.length > 0,
        key: !!key.value,
        value: !!value.value,
      };

      // 如果没有任何条件被设置，则直接返回原列表
      if (!Object.values(conditions).some(Boolean)) {
        return list.value;
      }

      return list.value.filter((item) => {
        let matchesAllConditions = true;

        // 根据设置的条件进行匹配
        if (conditions.color) {
          matchesAllConditions =
            matchesAllConditions && item.borderColor == color.value;
        }
        if (conditions.level) {
          matchesAllConditions =
            matchesAllConditions && item.level == level.value;
        }
        if (conditions.checkList) {
          const hasMarks = Array.isArray(item.marks) && item.marks.length > 0;
          matchesAllConditions =
            matchesAllConditions &&
            hasMarks &&
            checkList.value.includes(item.marks[0].shape_type);
        }
        if (conditions.key) {
          matchesAllConditions =
            matchesAllConditions && item.labelList.includes(key.value);
        }
        if (conditions.value) {
          matchesAllConditions =
            matchesAllConditions && item.valueList.includes(value.value);
        }

        return matchesAllConditions;
      });
    });
    return {
      cropExpandSize,
      color,
      lineWidth,
      lineStyle,
      level,
      lineColor,
      list,
      images,
      checkList,
      menuTreeList,
      key,
      value,
      createTime,
      dataList,
      lastNodeValue,
      moveTreeList,
      idsList,
      lastSubmitTime,
      lastChangeKeyNum,
      noOperationNum,
      changeKeyIdList,
      changeKeyList,
      workPackId,
      sign,
      processTypeName,
      workUserName,
      taskName,
      receiveTime,
      workUserDomain,
      rollbackKeys,
    };
  },
  {
    //严格模式 关闭 可以直接修改state的数据
    autoStrictMode: false,
    persist: {
      key: "main-store",
      storage: localStorage, // or sessionStorage
    },
  }
);
